﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;

namespace WoAiPao {

    /// <summary>
    /// 提供界面和数据的绑定。
    /// </summary>
    public class UIBinder {

        private object _dataSource;

        /// <summary>
        /// 绑定一个数据源。
        /// </summary>
        /// <param name="data"></param>
        public void bind<T>(T data) {
            _dataSource = data;
            loadData(data, HttpContext.Current.Request.Form);
        }

        /// <summary>
        /// 从指定集合载入指定对象的数据。
        /// </summary>
        /// <param name="result"></param>
        /// <param name="collection"></param>
        public static void loadData<T>(T obj, NameValueCollection collection) {
            if (collection == null) {
                return;
            }

            foreach (string key in collection) {

                // 获取属性。
                var prop = typeof(T).GetProperty(key, BindingFlags.Instance | BindingFlags.Public);
                if (prop == null || !prop.CanWrite) {
                    continue;
                }

                // 获取可用的值。
                var value = Circus.Utility.ConvertHelper.parseObject(collection[key], prop.PropertyType);
                if (value == null) {
                    continue;
                }

                // 设置属性值。
                prop.SetValue(obj, value, null);
            }
        }

        private HtmlString input(string type, string fieldName, string htmlAttributes = null) {
            StringBuilder sb = new StringBuilder();
            sb.Append("<input type=\"");
            sb.Append(type);
            sb.Append("\" name=\"");
            sb.Append(fieldName);
            sb.Append("\" id=\"");
            sb.Append(fieldName);
            sb.Append("\" value=\"");

            var prop = _dataSource.GetType().GetProperty(fieldName);
            if (prop != null) {
                var s = prop.GetValue(_dataSource, null);
                if (s != null) {
                    sb.Append(HttpUtility.HtmlAttributeEncode(s.ToString()));
                }
            }

            sb.Append("\" ");
            sb.Append(htmlAttributes);
            sb.Append(">");
            return new HtmlString(sb.ToString());
        }

        public HtmlString textBox(string fieldName, string htmlAttributes = null) {
            return input("text", fieldName, htmlAttributes);
        }

        public HtmlString email(string fieldName, string htmlAttributes = null) {
            return input("email", fieldName, htmlAttributes);
        }

        public HtmlString hidden(string fieldName, string htmlAttributes = null) {
            return input("hidden", fieldName, htmlAttributes);
        }

        public HtmlString password(string fieldName, string htmlAttributes = null) {
            StringBuilder sb = new StringBuilder();
            sb.Append("<input type=\"password\" name=\"");
            sb.Append(fieldName);
            sb.Append("\" id=\"");
            sb.Append(fieldName);
            sb.Append("\" autocomplete=\"off\"");
            sb.Append(htmlAttributes);
            sb.Append(">");
            return new HtmlString(sb.ToString());
        }

        public HtmlString checkbox(string fieldName, string htmlAttributes = null) {
            StringBuilder sb = new StringBuilder();
            sb.Append("<input type=\"checkbox\" name=\"");
            sb.Append(fieldName);
            sb.Append("\" id=\"");
            sb.Append(fieldName);
            sb.Append("\" ");

            var prop = _dataSource.GetType().GetProperty(fieldName);
            if (prop != null && prop.PropertyType == typeof(bool)) {
                if ((bool) prop.GetValue(_dataSource, null)) {
                    sb.Append(" checked=\"checked\" ");
                }
            }

            sb.Append(htmlAttributes);
            sb.Append(">");
            return new HtmlString(sb.ToString());
        }

    }

}